package com.nowcoder.community.actuator;/**
 * 功能描述
 *
 * @author 21775
 * @date 2022/10/11  15:51
 */

import com.nowcoder.community.utils.CommunityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 *@ClassName DatabaseEndPoint
 *@Description TODO 端点监控
 *@Author 21775
 *@Date 2022/10/11 15:51
 *@Version 1.0
 */
@Slf4j
@Component
@Endpoint(id = "database")
public class DatabaseEndPoint {
    @Resource
    private DataSource dataSource;

    @ReadOperation
    public String checkConnect(){
        try {
            Connection connection=dataSource.getConnection();
            return CommunityUtil.getJSONString(0,"获取连接成功");
        } catch (SQLException throwables) {
            log.info("获取连接失败："+throwables.getMessage());
            return CommunityUtil.getJSONString(1,"获取连接失败");
        }
    }
}
